-# locals: system, id_prefix, seen_ids

-model_underscore = 'system'

-is_root = false if local_assigns[:is_root].nil?
-is_editable = false if local_assigns[:is_editable].nil?

-seen_ids = [] if local_assigns[:seen_ids].nil?
-id_prefix = "object-#{system.id}" if local_assigns[:id_prefix].nil?

-seen_this = seen_ids.include?(system.id)

-controls = system.controls
-sub_systems = system.sub_systems

-expand_this = controls.count > 0 || (!seen_this && sub_systems.count > 0)

-summary_toggle_target = "#{id_prefix}-summary"
-objects_toggle_target = "#{id_prefix}-objects"

-main_toggle_target = expand_this ? objects_toggle_target : summary_toggle_target

%li{ :class => system.is_biz_process? ? "process" : "system", :'data-object-id' => system.id, :'data-object-type' => model_underscore }
  .item-main
    .openclose{ :class => !expand_this && 'lastchild' }
    .item-title
      .row-fluid
        =render :partial => 'object_leaf', :locals => { :instance => system, :object_type => 'compliance', :object_icon => "grcicon-#{system.is_biz_process? ? 'process' : 'system'}" }
      .item-actions
        -if is_editable
          %a.btn.btn-mini{ "rel" => "tooltip", "data-placement" => "left", "title" => "Edit this object", 'data-modal-class' => 'modal-wide', 'data-form-target' => 'refresh', "data-toggle" => "modal-ajax-form", :href => url_for(:id => system.id, :action => :edit, :controller => model_underscore.pluralize, :only_path => true) }
            %i.grcicon-edit
        %a.btn.btn-mini{ :href => list_edit_flow_system_systems_path(:parent_id => system.id), :'data-toggle' => 'modal-ajax-relationship-selector', :'data-modal-reset' => 'reset', :'data-list-target' => 'refresh', :rel => 'tooltip', :title => 'Link systems to this object', :'data-modal-class' => 'modal-wide' }
          %i.grcicon-link
        %a.btn.btn-mini{ :href => list_edit_flow_system_controls_path(:system_id => system.id), :'data-toggle' => 'modal-ajax-relationship-selector', :'data-modal-reset' => 'reset', :'data-list-target' => 'refresh', :rel => 'tooltip', :title => 'Link controls to this object', :'data-modal-class' => 'modal-wide' }
          %i.grcicon-link
        -if !is_root
          %a.btn.btn-mini{ "rel" => "tooltip", "data-placement" => "left", "title" => "Go to this object's page", :href => flow_system_path(system.id) }
            %i.grcicon-goto
  .item-content
    -if controls.count > 0
      %ul.tree-structure
        -controls.each_with_index do |control, i|
          -control_id_prefix = "#{id_prefix}-control-#{control.id}"
          -control_summary_target = "#{control_id_prefix}-summary"

          %li.control{ :'data-object-id' => control.id, :'data-object-type' => "control" }
            .item-main
              .openclose.lastchild
              .item-title
                .row-fluid
                  =render :partial => 'object_leaf', :locals => { :instance => control, :object_type => 'compliance', :object_icon => 'grcicon-control' }
                .item-actions
                  %a.btn.btn-mini{"rel" => "tooltip", "data-placement" => "left", "title" => "Go to this object's page", :href => flow_control_path(control.id) }
                    %i.grcicon-goto

    -if !seen_this && sub_systems.count > 0
      %ul.tree-structure
        -sub_systems.each_with_index do |sub_system, i|
          =render :partial => 'system_tree', :locals => { :system => sub_system, :id_prefix => "#{id_prefix}-sub-system-#{sub_system.id}", :seen_ids => seen_ids + [system.id] }
